package com.colodoo.wvs.manager.bug.service;

import com.colodoo.framework.base.abs.BaseService;
import com.colodoo.framework.easyui.Page;
import com.colodoo.framework.easyui.Tree;
import com.colodoo.wvs.manager.bug.model.Bug;
import com.colodoo.wvs.manager.bug.model.BugVO;
import com.colodoo.wvs.manager.bug.service.mapper.BugRepository;
import com.colodoo.wvs.manager.bug.service.mapper.BugSQLMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author colodoo
 * @date 2019-4-14 19:29:44
 * @description
 */
@Service
@Slf4j
public class BugService extends BaseService<Bug> {

    @Resource
    private BugSQLMapper sqlMapper;
    @Resource
    private BugRepository repository;

    /**
     * 新增数据
     *
     * @param model
     * @return
     */
    public void saveBug(Bug model) {
        model.setBugId(uuid());
        Date now = new Date();
        // 创建时间,更新时间,创建人,更新人
        model.setCreateTime(now);
        model.setUpdateTime(now);
        model.setParentBugId(null);
        String userId = this.getSessionObject().getUser().getUserId();
        model.setCreateUserId(userId);
        model.setUpdateUserId(userId);
        this.repository.save(model);
    }

    /**
     * 删除数据
     *
     * @param model
     * @return
     */
    public void deleteBug(Bug model) {
        this.repository.deleteById(model.getBugId());
    }

    /**
     * 更新数据
     *
     * @param model
     * @return
     */
    public void updateBug(Bug model) {
        Date now = new Date();
        model.setUpdateTime(now);
        String userId = this.getSessionObject().getUser().getUserId();
        model.setUpdateUserId(userId);
        this.repository.save(model);
    }

    /**
     * 根据id查找单条数据
     *
     * @param model
     * @return
     */
    public Bug queryById(Bug model) {
        return this.repository.getOne(model.getBugId());
    }

    /**
     * 查找列表
     *
     * @return
     */
    public List<BugVO> query(BugVO model) {
        return sqlMapper.getBugList(model);
    }

    /**
     * 查找分页列表
     *
     * @param page
     * @return
     */
    public PageInfo<BugVO> query(Page page, BugVO model) {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<BugVO> list = sqlMapper.getBugList(model);
        PageInfo<BugVO> pageInfo = new PageInfo<BugVO>(list);
        return pageInfo;
    }

    public List<Tree> getTreeMenu(String parentId) {
        /*if (parentId == null) {
            parentId = "";
        }*/
        List<Tree> list = new ArrayList<Tree>();
        BugVO bugVO = new BugVO();
        bugVO.setParentBugId(parentId);
        List<BugVO> bugs = this.sqlMapper.getBugList(bugVO);
        /*List<Bug> bugs = this.repository.findAll(
                Example.of(Bug.builder().parentBugId(parentId).build())
        );*/
        for (Bug bug : bugs) {
            Tree tree = Tree.builder()
                    .id(bug.getBugId())
                    .text(bug.getBugName())
                    .title(bug.getBugName())
                    .label(bug.getBugName())
                    .state("close")
                    .build();
            tree.setAttributes(bugs);
            List tempList = getTreeMenu(tree.getId());
            if (tempList.size() != 0) {
                tree.setChildren(tempList);
            }
            list.add(tree);
        }
        return list;
    }
}
